{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9c07c817",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tue Dec 26 03:15:30 2023       \n",
      "+-----------------------------------------------------------------------------+\n",
      "| NVIDIA-SMI 525.85.12    Driver Version: 525.85.12    CUDA Version: 12.0     |\n",
      "|-------------------------------+----------------------+----------------------+\n",
      "| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |\n",
      "| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |\n",
      "|                               |                      |               MIG M. |\n",
      "|===============================+======================+======================|\n",
      "|   0  NVIDIA A100-SXM...  On   | 00000001:00:00.0 Off |                    0 |\n",
      "| N/A   46C    P0   130W / 400W |  46140MiB / 81920MiB |    100%      Default |\n",
      "|                               |                      |             Disabled |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "|   1  NVIDIA A100-SXM...  On   | 00000002:00:00.0 Off |                    0 |\n",
      "| N/A   44C    P0   227W / 400W |  46524MiB / 81920MiB |    100%      Default |\n",
      "|                               |                      |             Disabled |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "|   2  NVIDIA A100-SXM...  On   | 00000003:00:00.0 Off |                    0 |\n",
      "| N/A   49C    P0   235W / 400W |  76884MiB / 81920MiB |    100%      Default |\n",
      "|                               |                      |             Disabled |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "|   3  NVIDIA A100-SXM...  On   | 00000004:00:00.0 Off |                    0 |\n",
      "| N/A   47C    P0   202W / 400W |  64734MiB / 81920MiB |    100%      Default |\n",
      "|                               |                      |             Disabled |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "|   4  NVIDIA A100-SXM...  On   | 0000000B:00:00.0 Off |                    0 |\n",
      "| N/A   48C    P0   213W / 400W |  74800MiB / 81920MiB |    100%      Default |\n",
      "|                               |                      |             Disabled |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "|   5  NVIDIA A100-SXM...  On   | 0000000C:00:00.0 Off |                    0 |\n",
      "| N/A   48C    P0   157W / 400W |  52930MiB / 81920MiB |    100%      Default |\n",
      "|                               |                      |             Disabled |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "|   6  NVIDIA A100-SXM...  On   | 0000000D:00:00.0 Off |                    0 |\n",
      "| N/A   46C    P0   212W / 400W |  74512MiB / 81920MiB |    100%      Default |\n",
      "|                               |                      |             Disabled |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "|   7  NVIDIA A100-SXM...  On   | 0000000E:00:00.0 Off |                    0 |\n",
      "| N/A   59C    P0   370W / 400W |  79320MiB / 81920MiB |     99%      Default |\n",
      "|                               |                      |             Disabled |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "                                                                               \n",
      "+-----------------------------------------------------------------------------+\n",
      "| Processes:                                                                  |\n",
      "|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |\n",
      "|        ID   ID                                                   Usage      |\n",
      "|=============================================================================|\n",
      "+-----------------------------------------------------------------------------+\n"
     ]
    }
   ],
   "source": [
    "!nvidia-smi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1a27f90a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip3 install scipy bitsandbytes --user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d4df3ba9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = '0'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1f47a9ac",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n",
    "import torch\n",
    "import json\n",
    "\n",
    "TORCH_DTYPE = 'bfloat16'\n",
    "nf4_config = BitsAndBytesConfig(\n",
    "    load_in_4bit=True,\n",
    "    bnb_4bit_quant_type='nf4',\n",
    "    bnb_4bit_use_double_quant=True,\n",
    "    bnb_4bit_compute_dtype=getattr(torch, TORCH_DTYPE)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4202af42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'fpf-1b-instructions-16k/checkpoint-5500'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from transformers.trainer_utils import get_last_checkpoint\n",
    "\n",
    "latest = get_last_checkpoint(\"fpf-1b-instructions-16k\")\n",
    "latest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c1b77c73",
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained(latest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2b2cace3",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = AutoModelForCausalLM.from_pretrained(\n",
    "    latest,\n",
    "    use_flash_attention_2 = True,\n",
    "    quantization_config = nf4_config\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2d8f2a4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# model = AutoModelForCausalLM.from_pretrained(\n",
    "#     latest,\n",
    "#     use_flash_attention_2 = True,\n",
    "#     torch_dtype = torch.bfloat16,\n",
    "# )\n",
    "# _ = model.cuda()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "dadcc2a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_mistral_chat(messages, function_call = None):\n",
    "\n",
    "    user_query = messages[-1]['content']\n",
    "\n",
    "    users, assistants = [], []\n",
    "    for q in messages[:-1]:\n",
    "        if q['role'] == 'user':\n",
    "            users.append(q['content'])\n",
    "        elif q['role'] == 'assistant':\n",
    "            assistants.append(q['content'])\n",
    "\n",
    "    texts = ['<s>']\n",
    "    \n",
    "    if function_call:\n",
    "        fs = []\n",
    "        for f in function_call:\n",
    "            f = json.dumps(f, indent=4)\n",
    "            fs.append(f)\n",
    "        fs = '\\n\\n'.join(fs)\n",
    "        texts.append(f'\\n[FUNCTIONCALL]\\n{fs}\\n')\n",
    "        \n",
    "    for u, a in zip(users, assistants):\n",
    "        texts.append(f'[INST] {u.strip()} [/INST] {a.strip()}</s>')\n",
    "\n",
    "    texts.append(f'[INST] {user_query.strip()} [/INST]')\n",
    "    prompt = ''.join(texts).strip()\n",
    "    return prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f579c29f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s> [INST] hey gila [/INST] hello, saya di sini untuk membantu! Saya boleh memberikan maklumat tentang pelbagai topik atau menjawab soalan anda tentang pelbagai subjek.</s>\n"
     ]
    }
   ],
   "source": [
    "s = \"\"\"\n",
    "hey gila\n",
    "\"\"\"\n",
    "\n",
    "messages = [\n",
    "    {'role': 'user', 'content': s.strip()},\n",
    "]\n",
    "prompt = parse_mistral_chat(messages)\n",
    "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')\n",
    "generate_kwargs = dict(\n",
    "    inputs,\n",
    "    max_new_tokens=128,\n",
    "    top_p=0.95,\n",
    "    top_k=50,\n",
    "    temperature=0.9,\n",
    "    do_sample=True,\n",
    "    num_beams=1,\n",
    ")\n",
    "r = model.generate(**generate_kwargs)\n",
    "print(tokenizer.decode(r[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a1aaae69",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s> [INST] objektif: Bahagian yang di dalam kurungan dalam ayat-ayat yang berikut mungkin mengandungi kesalahan bahasa dan mungkin juga tidak. Tandakan sama ada A, B atau C jika ayat itu mengandungi kesalahan dan tandakan D jika tiada kesalahan.\n",
      "soalan: Halimah percaya (percaya benar) anaknya tidak terbabit dengan tuduhan jenayah kolar putih.\n",
      "A. sungguh percaya benar\n",
      "B. teramat percaya benar\n",
      "C. sangat percaya benar\n",
      "D. percaya benar [/INST] D</s>\n"
     ]
    }
   ],
   "source": [
    "s = \"\"\"\n",
    "objektif: Bahagian yang di dalam kurungan dalam ayat-ayat yang berikut mungkin mengandungi kesalahan bahasa dan mungkin juga tidak. Tandakan sama ada A, B atau C jika ayat itu mengandungi kesalahan dan tandakan D jika tiada kesalahan.\n",
    "soalan: Halimah percaya (percaya benar) anaknya tidak terbabit dengan tuduhan jenayah kolar putih.\n",
    "A. sungguh percaya benar\n",
    "B. teramat percaya benar\n",
    "C. sangat percaya benar\n",
    "D. percaya benar\n",
    "\"\"\"\n",
    "\n",
    "messages = [\n",
    "    {'role': 'user', 'content': s.strip()}\n",
    "]\n",
    "prompt = parse_mistral_chat(messages)\n",
    "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')\n",
    "generate_kwargs = dict(\n",
    "    inputs,\n",
    "    max_new_tokens=128,\n",
    "    top_p=0.95,\n",
    "    top_k=50,\n",
    "    temperature=0.9,\n",
    "    do_sample=True,\n",
    "    num_beams=1,\n",
    ")\n",
    "r = model.generate(**generate_kwargs)\n",
    "print(tokenizer.decode(r[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "34c089da",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s> [INST] tolong tukar 10 ringgit ke usd [/INST] Untuk menukar 10 ringgit kepada dolar, langkah pertama ialah memasukkannya ke dalam dolar terdekat. Sebagai contoh, jika anda ingin menukar 10 ringgit kepada dolar, anda akan memasukkannya ke dalam dolar terdekat. Jika anda ingin menukar 10 dolar kepada mata wang lain, anda perlu menukarnya dahulu. Jika anda memerlukan contoh yang lebih terperinci, beritahu saya dan saya akan cuba membantu anda dengan lebih baik.</s>\n"
     ]
    }
   ],
   "source": [
    "messages = [\n",
    "    {'role': 'user', 'content': 'tolong tukar 10 ringgit ke usd'}\n",
    "]\n",
    "prompt = parse_mistral_chat(messages)\n",
    "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')\n",
    "generate_kwargs = dict(\n",
    "    inputs,\n",
    "    max_new_tokens=128,\n",
    "    top_p=0.95,\n",
    "    top_k=50,\n",
    "    temperature=0.9,\n",
    "    do_sample=True,\n",
    "    num_beams=1,\n",
    ")\n",
    "r = model.generate(**generate_kwargs)\n",
    "print(tokenizer.decode(r[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fded8c36",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s> [INST] knowledge base: Getting started Introduction Quickstart TypeScript License Changelog Integration guides React Next.js Vue Nuxt Svelte Angular NEW Astro Remix Meteor.js NEW Gatsby SolidJS Qwik Laravel NEW Symfony Rails Phoenix Django Flask Customize Configuration Dark mode Theming Colors Icons Optimization Components Accordion Alerts Avatar Badge Banner Bottom Navigation Breadcrumb Buttons Button Group Card Carousel Device Mockups Drawer Dropdowns Footer Forms Gallery Indicators Jumbotron KBD List Group Mega menu Modal Navbar Pagination Popover Progress Rating Sidebar Skeleton Speed Dial Spinner Stepper Tables Tabs Timeline Toast Tooltips Typography Video Forms Input Field File Input Search Input Select Textarea Checkbox Radio Toggle Range Floating Label Typography Headings Paragraphs Blockquote Images Lists Links Text HR Plugins Charts NEW Datepicker\n",
      "user: apa knowledge base awak?\n",
      "bot:Pangkalan pengetahuan saya termasuk pengenalan, panduan pantas, lesen, perubahan log, dan panduan penyepaduan dengan pelbagai rangka seperti React, Next.js, Vue, Nuxt, Svelte, Angular, Astro, Remix, Meteor.js, Gatsby, SolidJS, Qwik, Laravel, Symfony, Rails, Phoenix, Django, Flask, dan lain-lain.\n",
      "\n",
      "Selain itu, ia juga merangkumi panduan untuk mengubah suai konfigurasi, menggunakan tema gelap, menetapkan warna, menggunakan ikon, mengoptimumkan komponen, dan pelbagai komponen seperti akordion, alert, avatar, badge, banner, navigasi bawah, serbuk roti, butang, kad, karusel, mockup peranti, laci, dropdown, footer, form, galeri, penunjuk, jumbotron, KBD, senarai kumpulan, mega menu, modal, navbar, pagination, popover, kemajuan, penarafan, sidebar, rangka, speed dial, spinner, stepper, tables, tabs, timeline, toast, tooltips, typography, video, dan banyak lagi.\n",
      "\n",
      "Terdapat juga pemalam seperti carta dan datepicker dalam pangkalan pengetahuan saya. Jika terdapat soalan yang lebih khusus tentang sesuatu dalam pangkalan pengetahuan ini, sila beritahu saya. Saya sedia membantu.\n",
      "\n",
      "Explain step-by-step very details with relationships why `bot` respond like that to `user` given on `knowledge base` input? [/INST] `bot` bertindak sebagai `penjaga kebenaran` dalam pangkalan pengetahuan `knowledge base` untuk membantu `user` dalam membuat pertanyaan atau tindakan berdasarkan input yang diberikan kepada `bot`. Ini membantu `bot` dalam memahami dan mengambil tindakan yang sesuai berdasarkan input pengguna.\n",
      "\n",
      "Contoh:\n",
      "Dalam bahasa Melayu:\n",
      "```javascript\n",
      "user: Apakah pengetahuan asas anda?\n",
      "bot: Apakah pengetahuan asas anda?\n",
      "```\n",
      "\n",
      "Dalam bahasa Inggeris:\n",
      "```javascript\n",
      "user: Apakah pengetahuan asas anda?\n",
      "```\n",
      "\n",
      "Dalam bahasa Inggeris:\n",
      "```javascript\n",
      "user: Apakah pengetahuan asas anda?\n",
      "```\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "c = \"\"\"\n",
    "knowledge base: Getting started Introduction Quickstart TypeScript License Changelog Integration guides React Next.js Vue Nuxt Svelte Angular NEW Astro Remix Meteor.js NEW Gatsby SolidJS Qwik Laravel NEW Symfony Rails Phoenix Django Flask Customize Configuration Dark mode Theming Colors Icons Optimization Components Accordion Alerts Avatar Badge Banner Bottom Navigation Breadcrumb Buttons Button Group Card Carousel Device Mockups Drawer Dropdowns Footer Forms Gallery Indicators Jumbotron KBD List Group Mega menu Modal Navbar Pagination Popover Progress Rating Sidebar Skeleton Speed Dial Spinner Stepper Tables Tabs Timeline Toast Tooltips Typography Video Forms Input Field File Input Search Input Select Textarea Checkbox Radio Toggle Range Floating Label Typography Headings Paragraphs Blockquote Images Lists Links Text HR Plugins Charts NEW Datepicker\n",
    "user: apa knowledge base awak?\n",
    "bot:Pangkalan pengetahuan saya termasuk pengenalan, panduan pantas, lesen, perubahan log, dan panduan penyepaduan dengan pelbagai rangka seperti React, Next.js, Vue, Nuxt, Svelte, Angular, Astro, Remix, Meteor.js, Gatsby, SolidJS, Qwik, Laravel, Symfony, Rails, Phoenix, Django, Flask, dan lain-lain.\n",
    "\n",
    "Selain itu, ia juga merangkumi panduan untuk mengubah suai konfigurasi, menggunakan tema gelap, menetapkan warna, menggunakan ikon, mengoptimumkan komponen, dan pelbagai komponen seperti akordion, alert, avatar, badge, banner, navigasi bawah, serbuk roti, butang, kad, karusel, mockup peranti, laci, dropdown, footer, form, galeri, penunjuk, jumbotron, KBD, senarai kumpulan, mega menu, modal, navbar, pagination, popover, kemajuan, penarafan, sidebar, rangka, speed dial, spinner, stepper, tables, tabs, timeline, toast, tooltips, typography, video, dan banyak lagi.\n",
    "\n",
    "Terdapat juga pemalam seperti carta dan datepicker dalam pangkalan pengetahuan saya. Jika terdapat soalan yang lebih khusus tentang sesuatu dalam pangkalan pengetahuan ini, sila beritahu saya. Saya sedia membantu.\n",
    "\n",
    "Explain step-by-step very details with relationships why `bot` respond like that to `user` given on `knowledge base` input?\n",
    "\"\"\"\n",
    "messages = [\n",
    "    {'role': 'user', 'content': c.strip()}\n",
    "]\n",
    "prompt = parse_mistral_chat(messages)\n",
    "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')\n",
    "generate_kwargs = dict(\n",
    "    inputs,\n",
    "    max_new_tokens=128,\n",
    "    top_p=0.95,\n",
    "    top_k=50,\n",
    "    temperature=0.9,\n",
    "    do_sample=True,\n",
    "    num_beams=1,\n",
    ")\n",
    "r = model.generate(**generate_kwargs)\n",
    "print(tokenizer.decode(r[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "496b2fae",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s> [INST] CREATE TABLE pelajar (\n",
      "   id SERIAL PRIMARY KEY,\n",
      "   name VARCHAR(50),\n",
      "   email VARCHAR(50),\n",
      "   student_number VARCHAR(50),\n",
      "   course VARCHAR(50),\n",
      "   start_date DATE,\n",
      "   end_date DATE,\n",
      "   grade VARCHAR(50)\n",
      ")\n",
      "\n",
      "berdasarkan table definition, bina sql query untuk filter pelajar yang grade C keatas [/INST] Anda boleh menggunakan kueri berikut untuk mendapatkan gred pelajar C ke atas dalam jadual \"pelajar\":\n",
      "\n",
      "```sql\n",
      "SELECT gred FROM pelajar ORDER BY gred DESC LIMIT 1;\n",
      "```\n",
      "\n",
      "Untuk membina kueri SQL yang lebih kompleks:\n",
      "\n",
      "```sql\n",
      "SELECT gred FROM pelajar\n",
      "ORDER BY gred DESC\n",
      "LIMIT 1;\n",
      "```\n",
      "\n",
      "Kueri SQL yang lengkap akan mengembalikan gred pelajar C ke atas, di mana gred adalah nilai gred dari jadual \"pelajar\" dalam jadual \"pelajar\". Dalam kueri di atas, jika tiada pelajar yang memenuhi kriteria, fungsi `ORDER BY` akan mengembalikan gred secara langsung. Untuk mendapatkan gred pelajar C ke atas, kueri pertama akan menggunakan klausa `ORDER BY` dan kemudian menggunakan klausa `LIMIT` untuk mengehadkan hasil kepada hanya satu baris.</s>\n"
     ]
    }
   ],
   "source": [
    "s = \"\"\"\n",
    "CREATE TABLE pelajar (\n",
    "   id SERIAL PRIMARY KEY,\n",
    "   name VARCHAR(50),\n",
    "   email VARCHAR(50),\n",
    "   student_number VARCHAR(50),\n",
    "   course VARCHAR(50),\n",
    "   start_date DATE,\n",
    "   end_date DATE,\n",
    "   grade VARCHAR(50)\n",
    ")\n",
    "\n",
    "berdasarkan table definition, bina sql query untuk filter pelajar yang grade C keatas\n",
    "\"\"\"\n",
    "\n",
    "messages = [\n",
    "    {'role': 'user', 'content': s.strip()}\n",
    "]\n",
    "prompt = parse_mistral_chat(messages)\n",
    "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')\n",
    "generate_kwargs = dict(\n",
    "    inputs,\n",
    "    max_new_tokens=256,\n",
    "    top_p=0.95,\n",
    "    top_k=50,\n",
    "    temperature=0.9,\n",
    "    do_sample=True,\n",
    "    num_beams=1,\n",
    ")\n",
    "r = model.generate(**generate_kwargs)\n",
    "print(tokenizer.decode(r[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "849aaec6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s> [INST] create simple postgres table untuk student [/INST] Untuk mencipta jadual PostgreSQL yang ringkas untuk pelajar dalam bahasa Melayu, ikuti langkah-langkah berikut:\n",
      "\n",
      "1. Buka teks editor anda.\n",
      "\n",
      "2. Letakkan fail \"postgres.sql\" sebagai destinasi.\n",
      "\n",
      "3. Kemudian, buka fail yang baru anda cipta.\n",
      "\n",
      "4. Masukkan butiran yang diperlukan.\n",
      "\n",
      "5. Setelah berjaya membuat sambungan, tekan 'Fail sambungan baru telah dibuat'.\n",
      "\n",
      "6. Buka pangkalan data PostgreSQL anda, dan klik \"Mula\" atau \"Pilih pangkalan data\".\n",
      "\n",
      "7. Di bahagian bawah jadual \"pelajar\" (biasanya dalam fail \"nama_fail.sql\"), anda akan menemui satu lajur yang mungkin anda ingin paparkan.\n",
      "\n",
      "8. Klik \"Jalankan\" untuk menjalankan sambungan.\n",
      "\n",
      "9. Sambungan anda kini akan dibuka dalam pangkalan data.\n",
      "\n",
      "Itu sahaja, anda telah mencipta jadual untuk pelajar anda dengan berjaya.\n",
      "\n",
      "Pastikan untuk memeriksa dokumentasi sambungan pangkalan data anda untuk maklumat lanjut.</s>\n"
     ]
    }
   ],
   "source": [
    "messages = [\n",
    "    {'role': 'user', 'content': 'create simple postgres table untuk student'}\n",
    "]\n",
    "prompt = parse_mistral_chat(messages)\n",
    "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')\n",
    "generate_kwargs = dict(\n",
    "    inputs,\n",
    "    max_new_tokens=256,\n",
    "    top_p=0.95,\n",
    "    top_k=50,\n",
    "    temperature=0.9,\n",
    "    do_sample=True,\n",
    "    num_beams=1,\n",
    ")\n",
    "r = model.generate(**generate_kwargs)\n",
    "print(tokenizer.decode(r[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "23ef0eec",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s> [INST] contoh 2d convolution guna numpy [/INST] Anda boleh menggunakan NumPy untuk menghasilkan matriks rawak menggunakan operasi \"conv\" pada matriks dan kemudian menggunakan `np.zeros` untuk mengisi matriks dengan nilai 0 pada setiap elemennya. Berikut adalah contoh kod:\n",
      "\n",
      "```python\n",
      "import numpy as np\n",
      "import matplotlib.pyplot as plt\n",
      "\n",
      "# Matriks dengan nilai 0 pada setiap elemennya\n",
      "mat_np = np.zeros((2, 4))\n",
      "\n",
      "# Menambahkannya ke plot\n",
      "plt.plot([1, 2, 3, 4], [1, 1, 2, 3])\n",
      "\n",
      "# Menetapkan semula plot\n",
      "plt.show()\n",
      "```\n",
      "\n",
      "Dalam contoh ini, kami menggunakan `np.zeros` untuk mengisi matriks rawak dengan nilai 0 pada setiap elemennya. Kemudian, dengan `plt.plot`, plot akan dicipta dengan `plt.show`.\n",
      "\n",
      "Anda boleh menukar matriks dengan nilai lain seperti dalam contoh yang diberikan.</s>\n"
     ]
    }
   ],
   "source": [
    "messages = [\n",
    "    {'role': 'user', 'content': 'contoh 2d convolution guna numpy'}\n",
    "]\n",
    "prompt = parse_mistral_chat(messages)\n",
    "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')\n",
    "generate_kwargs = dict(\n",
    "    inputs,\n",
    "    max_new_tokens=256,\n",
    "    top_p=0.95,\n",
    "    top_k=50,\n",
    "    temperature=0.9,\n",
    "    do_sample=True,\n",
    "    num_beams=1,\n",
    ")\n",
    "r = model.generate(**generate_kwargs)\n",
    "print(tokenizer.decode(r[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a4e20b98",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s> [INST] kwsp tu apa [/INST] KWSP merujuk kepada Kumpulan Wang Simpanan Pekerja.</s>\n"
     ]
    }
   ],
   "source": [
    "messages = [\n",
    "    {'role': 'user', 'content': 'kwsp tu apa'}\n",
    "]\n",
    "prompt = parse_mistral_chat(messages)\n",
    "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')\n",
    "generate_kwargs = dict(\n",
    "    inputs,\n",
    "    max_new_tokens=256,\n",
    "    top_p=0.95,\n",
    "    top_k=50,\n",
    "    temperature=0.9,\n",
    "    do_sample=True,\n",
    "    num_beams=1,\n",
    ")\n",
    "r = model.generate(**generate_kwargs)\n",
    "print(tokenizer.decode(r[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "87553fcb",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s> [INST] kenapa maybank leceh sangat [/INST] Maybank memang menghadapi cabaran dengan peraturan dan garis panduan yang kompleks, tetapi ia boleh menjadi mudah bagi sesetengah orang. Walau bagaimanapun, terdapat beberapa perkara yang perlu diperbaiki untuk bank, seperti:\n",
      "\n",
      "1. Perubahan dalam waktu operasi: Maybank mungkin memperkenalkan beberapa perubahan dalam waktu operasi bank, seperti mengurangkan waktu buka atau menghentikan operasi, atau memperkenalkan waktu perbankan tambahan.\n",
      "\n",
      "2. Kekurangan maklumat: Maybank mungkin memerlukan maklumat tertentu semasa membuka akaun atau melakukan transaksi, yang boleh menyebabkan pelanggan yang tidak layak perlu mendapatkan penjelasan lanjut atau penjelasan mengenai perubahan ini.\n",
      "\n",
      "3. Perkhidmatan pelanggan: Maybank perlu terus meningkatkan perkhidmatan pelanggan mereka dan memastikan terdapat saluran komunikasi yang baik antara pelanggan dan kaunter. Ini boleh membantu untuk mengelakkan kekecewaan pelanggan.\n",
      "\n",
      "4. Penempatan fungsi: Maybank mungkin perlu memperbaiki penempatan fungsi yang kurang cekap dan menggantikan fungsi yang lebih cekap. Ini mungkin bermakna menggantikan fungsi yang memerlukan masa atau sumber, atau menambah ciri baharu.\n",
      "\n",
      "5. Pengurangan kos operasi: Maybank mungkin mengambil langkah untuk mengurangkan kos operasi, seperti mengurangkan kos overhead dan meningkatkan kecekapan, atau menambah baik algoritma pemprosesan transaksi.\n",
      "\n",
      "6. Penyelesaian pertikaian: Maybank mungkin perlu meningkatkan penyelesaian pertikaian untuk pelanggan, seperti menyediakan bantuan yang lebih cekap kepada pelanggan yang menghadapi kesukaran membuat transaksi atau menyelesaikan isu yang berkaitan dengan pelanggan\n"
     ]
    }
   ],
   "source": [
    "messages = [\n",
    "    {'role': 'user', 'content': 'kenapa maybank leceh sangat'}   \n",
    "]\n",
    "prompt = parse_mistral_chat(messages)\n",
    "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')\n",
    "generate_kwargs = dict(\n",
    "    inputs,\n",
    "    max_new_tokens=256,\n",
    "    top_p=0.95,\n",
    "    top_k=50,\n",
    "    temperature=0.9,\n",
    "    do_sample=True,\n",
    "    num_beams=1,\n",
    ")\n",
    "r = model.generate(**generate_kwargs)\n",
    "print(tokenizer.decode(r[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "954645cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s> [INST] kenapa najib razak korup [/INST] Najib Razak dianggap sebagai salah satu perompak terburuk dalam sejarah Malaysia kerana peranannya yang didakwa terlibat dalam skandal 1Malaysia Development Berhad (1MDB). Dia didakwa telah bersekongkol dan memanipulasi wang negara Malaysia untuk kepentingan peribadinya, yang kemudiannya mengakibatkan kerugian berbilion dolar.\n",
      "\n",
      "Berikut adalah beberapa sebab mengapa Najib Razak telah dikaitkan dengan skandal 1MDB dan tuduhan rasuahnya:\n",
      "\n",
      "1. **Penggunaan Dana Awam**: Najib Razak dituduh menggunakan dana awam untuk kepentingan peribadinya dengan cara yang paling membimbangkan. Beliau dituduh menggunakan dana yang berasal dari 1MDB untuk membiayai parti politiknya, Pertubuhan Kebangsaan Melayu Bersatu (UMNO), dan untuk memberi sogokan kepada orang ramai.\n",
      "\n",
      "2. **Menyembunyikan Rasuah**: Skandal 1MDB dilihat sebagai salah satu kes salah laku kewangan terbesar dalam sejarah. Najib Razak dituduh gagal memberikan jawapan yang memuaskan kepada soalan mengenai skandal 1MDB dan bagaimana ia memberi kesan kepada pembangunan negara.\n",
      "\n",
      "3. **Membayangi Siasatan**: Skandal 1MDB juga menimbulkan kebimbangan mengenai hubungan rapat antara Najib Razak dan pihak berkuasa antarabangsa yang terlibat dalam penyiasatan. Penyiasat dari pelbagai negara, termasuk Amerika Syarikat, Switzerland, dan Singapura, mendakwa bahawa sejumlah besar wang telah disel\n"
     ]
    }
   ],
   "source": [
    "messages = [\n",
    "    {'role': 'user', 'content': 'kenapa najib razak korup'}   \n",
    "]\n",
    "prompt = parse_mistral_chat(messages)\n",
    "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')\n",
    "generate_kwargs = dict(\n",
    "    inputs,\n",
    "    max_new_tokens=256,\n",
    "    top_p=0.95,\n",
    "    top_k=50,\n",
    "    temperature=0.9,\n",
    "    do_sample=True,\n",
    "    num_beams=1,\n",
    ")\n",
    "r = model.generate(**generate_kwargs)\n",
    "print(tokenizer.decode(r[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "59b0c973",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s> [INST] 1+1 / 10 [/INST] 1+1 = 1\n",
      "10 = 10\n",
      "Oleh itu, jawapannya ialah 1</s>\n"
     ]
    }
   ],
   "source": [
    "messages = [\n",
    "    {'role': 'user', 'content': '1+1 / 10'}   \n",
    "]\n",
    "prompt = parse_mistral_chat(messages)\n",
    "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')\n",
    "generate_kwargs = dict(\n",
    "    inputs,\n",
    "    max_new_tokens=256,\n",
    "    top_p=0.95,\n",
    "    top_k=50,\n",
    "    temperature=0.9,\n",
    "    do_sample=True,\n",
    "    num_beams=1,\n",
    ")\n",
    "r = model.generate(**generate_kwargs)\n",
    "print(tokenizer.decode(r[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c0f7fe4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s> [INST] babi boleh terbang tak [/INST] Saya tidak makan daging babi. Anda boleh makan, tetapi tidak boleh terbang.</s>\n"
     ]
    }
   ],
   "source": [
    "messages = [\n",
    "    {'role': 'user', 'content': 'babi boleh terbang tak'}   \n",
    "]\n",
    "prompt = parse_mistral_chat(messages)\n",
    "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')\n",
    "generate_kwargs = dict(\n",
    "    inputs,\n",
    "    max_new_tokens=256,\n",
    "    top_p=0.95,\n",
    "    top_k=50,\n",
    "    temperature=0.9,\n",
    "    do_sample=True,\n",
    "    num_beams=1,\n",
    ")\n",
    "r = model.generate(**generate_kwargs)\n",
    "print(tokenizer.decode(r[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9c345bfc",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s> [INST] apa tu kata hubung [/INST] Kata hubung ialah perkataan yang digunakan untuk menyambungkan dua klausa atau ayat. Ia boleh jadi \"apa\", \"di mana\", \"bilakah\", \"kenapa\", \"mengapa\", dan sebagainya. Contoh: Dia datang ke pasar.</s>\n"
     ]
    }
   ],
   "source": [
    "messages = [\n",
    "    {'role': 'user', 'content': 'apa tu kata hubung'}   \n",
    "]\n",
    "prompt = parse_mistral_chat(messages)\n",
    "inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')\n",
    "generate_kwargs = dict(\n",
    "    inputs,\n",
    "    max_new_tokens=256,\n",
    "    top_p=0.95,\n",
    "    top_k=50,\n",
    "    temperature=0.9,\n",
    "    do_sample=True,\n",
    "    num_beams=1,\n",
    ")\n",
    "r = model.generate(**generate_kwargs)\n",
    "print(tokenizer.decode(r[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51da13ed",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
